define(function (require) {

    //include
    require('./list-tpl');

    var orderDetail = require('../../../c/order-detail/index');

    var hongbaoDetail = require('../../../c/hongbao/index');

    var rePreOrder = require('../../../c/preorder-resend/index');

    var refund = require('../../../c/refund/index');

    var modifyAddress = require('../../../c/modify-address/index');

    var listTpl = juicer.templates['list.juicer.html'];

    var Detail = {

        init: function () {
            this.bindEvents();
        },

        /**
         * 取消订单
         * @param dataString
         */
        removeOrderItem: function (node, dataString) {
            ///trade/crm/order/cancelOrder
            var self = this;
            CRM.request({
                "type": 'POST',
                "url": CRM.API.cancelOrder,
                "dataType": "json",
                contentType: "application/json",
                "data": dataString,
                "success": function (req) {
                    if (req.responseCode && req.status) {
                        var html = listTpl({data: req.entry});
                        node.html(html);
                        CRM.showDialog(req.message);
                    } else {
                        CRM.showDialog(req.message);
                    }
                }
            });
        },
        //拼团重发
        rePinTuan: function (target, orderId) {
            CRM.request({
                "type": 'POST',
                "url": CRM.getOuterUrl("/trade/groupOrder/resendOrder"),
                "dataType": "json",
                contentType: "application/x-www-form-urlencoded",
                "data": {
                    "bizOrderId": orderId
                },
                "success": function (req) {
                    if (req.responseCode && req.status) {
                        $(target).hide();
                        CRM.showDialog(req.message);
                    } else {
                        CRM.showDialog(req.message);
                    }
                }
            });
        },
        /**
         * 发送红包界面
         * @param orderId
         * @param mobile
         */
        showHongbao: function (orderId, mobile) {
            hongbaoDetail.init(orderId, mobile);
        },

        bindEvents: function () {
            var self = this;

            //渲染订单
            $(document).on('click', '.order-item', function (ev) {
                ev.preventDefault();
                var bizOrderId = $(this).attr('data-id');
                orderDetail.init(bizOrderId);
            });

            //取消订单
            $(document).on('click', '.J_RemoveOrder', function (ev) {
                //ev.preventdefault();
                var target = ev.target;
                var bizOrderId = $(target).attr('data-id');
                var parentNode = $(target).parents("td");
                if (confirm('确认是否取消订单')) {
                    self.removeOrderItem(parentNode, JSON.stringify({
                        "bizOrderId": bizOrderId
                    }));
                }
            });

            //红包发送
            $(document).on('click', '.J_sendHongbao', function (ev) {
                //ev.preventdefault();
                var target = ev.target;
                var orderId = $(target).attr('data-id');
                var mobile = $(target).attr('data-mobile');
                var orderMobile = $(target).attr('data-orderMobile');
                hongbaoDetail.init(orderId, mobile,orderMobile);
            });

            //拼团重发
            $(document).on('click', '.J_pinTuan', function (ev) {
                var target = ev.target;
                var orderId = $(target).attr('data-id');
                if (confirm('确认是否拼团重发')) {
                    self.rePinTuan(target, orderId);
                }
            });


            //退款
            $(document).on('click', '.J_Refund', function (ev) {
                var target = ev.target;
                var orderId = $(target).attr('data-id');
                refund.init(orderId);
            });
            //重发
            $(document).on('click', '.J_PreSale', function (ev) {
                var target = ev.target;
                var orderId = $(target).attr('data-id');
                var mobile = $(target).attr('data-mobile');
                var buyAmount = $(target).attr('data-buyAmount');
                var addressSnapshot = $(target).attr('data-addressSnapshot');
                var district = $(target).attr('data-district');
                var cityCode = $(target).attr('data-cityCode');
                var community = $(target).attr('data-community');
                var orderType = $(target).attr('data-orderType');

                rePreOrder.init({
                    orderId: orderId,
                    mobile: mobile,
                    buyAmount: buyAmount,
                    addressSnapshot: addressSnapshot,
                    district:district,
                    cityCode:cityCode,
                    community:community,
                    orderType:orderType
                });

            });
            //修改发货地址
            $(document).on('click', '.J_ModeifyAddress', function (ev) {
                var target = ev.target;
                var orderId = $(target).attr('data-id');
                var mobile = $(target).attr('data-mobile');
                var buyAmount = $(target).attr('data-buyAmount');
                var addressSnapshot = $(target).attr('data-addressSnapshot');
                var district = $(target).attr('data-district');
                var cityCode = $(target).attr('data-cityCode');
                var community = $(target).attr('data-community');
                var orderType = $(target).attr('data-orderType');

                modifyAddress.init({
                    orderId: orderId,
                    mobile: mobile,
                    buyAmount: buyAmount,
                    addressSnapshot: addressSnapshot,
                    district:district,
                    cityCode:cityCode,
                    community:community,
                    orderType:orderType
                });

            });
        }
    };
    return Detail;
});